from flask_restful import Resource, Api
from flask import Blueprint, request, session
from libs.response import generate_response
from model.models import User, UserAddress, db

addressapi_bp = Blueprint("addressapi", __name__)
api = Api(addressapi_bp)


class AddressApi(Resource):

    def get(self):
        tmp = []
        account = session.get("account")
        user = User.query.filter(User.account == account).first()  # 根据session找到用户

        address = UserAddress.query.filter(UserAddress.user_id == user.id).all()  # 根据用户id找到对应的收货地址

        for i in address:
            tmp.append(i.to_json())  # 根据订单中的商品id找到对应的商品

        return generate_response(data=tmp, message="get address success", status_code=2000)

    def post(self):
        account = session.get("account")
        user = User.query.filter(User.account == account).first()  # 根据session找到用户

        address_info = request.get_json()  # 获取添加的用户信息
        if address_info is None:
            return generate_response(message="传递的参数为空", status_code=4000)
        user_address = UserAddress()
        user_address.user_id = user.id

        try:
            user_address.name = address_info.get("name")
            user_address.phone = address_info.get("phone")
            user_address.address = address_info.get("address")

            db.session.add(user_address)
            db.session.commit()

            return generate_response(message="add address success", status_code=2000)
        except Exception:
            return generate_response(message="传递的参数有误", status_code=4000)

    def put(self):
        account = session.get("account")
        user = User.query.filter(User.account == account).first()
        user_address = UserAddress.query.filter(UserAddress.user_id == user.id).all()  # 获取当前用户所有的地址
        address_info = request.get_json()  # 获取修改信息

        try:
            for i in user_address:
                if i.id == address_info["id"]:
                    address = UserAddress.query.filter(UserAddress.id == i.id).first()  # 找到需要更新的地址
                    for k in address_info:  # 根据传递的参数修改值
                        if k == "name":
                            address.name = address_info.get(k)
                        if k == "phone":
                            address.phone = address_info.get(k)
                        if k == "address":
                            address.address = address_info.get(k)

                    db.session.commit()
                    return generate_response(message="address update success", status_code=2000)
            else:
                return generate_response(message="address not found", status_code=4004)

        except Exception:
            return generate_response(message="请传递要修改地址的id", status_code=4000)

    def delete(self, id_=None):
        if id_ is None:
            return generate_response(message="传递的id为空", status_code=4000)
        try:
            id_ = int(id_)
        except Exception:
            return generate_response(message="传递的参数有误", status_code=4000)
        user_address = UserAddress.query.filter(UserAddress.id == id_).first()
        print(user_address, "+++++++++++++++++++++++++++++++++")
        print("---------------------------")
        if user_address is None:
            return generate_response(message="id not found", status_code=4004)
        db.session.delete(user_address)
        db.session.commit()
        return generate_response(message="drop address success", status_code=2000)


api.add_resource(AddressApi, "/addressapi", "/addressapi/")
api.add_resource(AddressApi, "/addressapi/<id_>", endpoint='addressapiid')
